package com.gitee.candy.thread;

/**
 * @author suYanLong
 * @date 2022年10月21日 15:35
 */
public class Test {

    static final Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {

        Thread thread1 = new Thread(getRunnable(), "thread1");
        Thread thread2 = new Thread(getRunnable(), "thread2");
        Thread thread3 = new Thread(getRunnable(), "thread3");
        Thread thread4 = new Thread(getRunnable(), "thread4");
        Thread thread5 = new Thread(getRunnable(), "thread5");

        thread1.start();
//        System.out.println("启动 thread1");
//        Thread.sleep(1000l);

        thread2.start();
//        System.out.println("启动 thread2");
//        Thread.sleep(1000l);

        thread3.start();
//        System.out.println("启动 thread3");
//        Thread.sleep(1000l);

        thread4.start();
//        System.out.println("启动 thread4");
//        Thread.sleep(1000l);

        thread5.start();
//        System.out.println("启动 thread5");
//        Thread.sleep(1000l);

        Thread.sleep(2000l * 10);
    }

    public static Runnable getRunnable() {
        return () -> {
            System.out.println(Thread.currentThread().getName() + "开始执行");
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName() + "获取到锁");
                try {
                    Thread.sleep(1000l * 10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        };
    }

}
